<?php

namespace app\common\model;
use Think\Db;
use think\Cache;
/**
 * 	点赞模型
 */
class Favor extends Base{
	//添加
	public function add($uid,$bookid,$chapter_id=0,$model) {
		$data['uid'] = $uid;
		$data['create_time'] = time();
		$data['model'] = $model;
		$data['cartoon_id'] = $bookid;
		if($chapter_id){
			$data['chapter_id'] = $chapter_id;
		}
		$res = Db::name('favor')->insert($data);
		if($res){
			Db::name('cartoon')->where(array('id'=>$bookid))->setInc('goodnum');
			if($model==2){
				Db::name('volume')->where(array('id'=>$chapter_id))->setInc('goodnum');
			}
		}
		return $res;
		
	}
	//删除
	public function del($uid,$bookid,$chapter_id,$model){
		$res = db('favor')->where(array('model'=>$model,'cartoon_id'=>$bookid,'chapter_id'=>$chapter_id,'uid'=>$uid))->delete();
		if($res){
			Db::name('cartoon')->where(array('id'=>$bookid))->setDec('goodnum');
			if($model==2){
				Db::name('volume')->where(array('id'=>$chapter_id))->setDec('goodnum');
			}
		}
		return $res;
	}
	
	//查询
	public function find($uid,$bookid,$chapter_id,$model) {		
		
		$favor = db('favor')->where(array('model'=>$model,'cartoon_id'=>$bookid,'chapter_id'=>$chapter_id,'uid'=>$uid))->find();
		if($favor){
			return 1;
		}else{
			return 0;
		}
		
	}

}